#! /usr/bin/env node

const { program} = require('commander');
const inquirerObj = require('inquirer')
const path = require('path')
const ora = require('ora')
const clone = require('git-clone');
const fs = require('fs-extra')
const { red, green, cyan, bold, dim, yellow, magenta } = require('picocolors')
const inquirer = inquirerObj.default

const packageJson = require(path.join(__dirname, '../package.json'))

// 仓库地址
const repos = {
    // 'React-TypeScript': 'https://github.com/liz-q/react-ts-template.git',
    'React-TypeScript': 'git@coding.jd.com:mall_fe/project-template.git',
    'React-JavaScript': '',
    'Vue-TypeScript': '',
    'Vue-JavaScript': '',
}
program
    .name('create-jd-web')
    .description(green('构建一个规范的前端项目'))
    .version(packageJson.version);

program.parse();


async function start () {
    try {
        const answers = await inquirer.prompt( [
            {
                type: 'input',
                name: 'projectName',
                message: 'Project name：',
                validate (value) {
                    if (value) {
                        const targetPath = path.join(process.cwd(), value)
                        if (fs.pathExistsSync(targetPath)) {
                            return yellow(`Target directory "${value}" is not empty，please input again.`)
                        }
                        return true
                    }
                    return false
                }
            },
            {
                type: 'list',
                name: 'framework',
                message: 'Select a framework',
                choices: [
                    'React',
                    {
                        name: 'Vue',
                        disabled: 'Unavailable at this time',
                    },
                ]
            },
            {
                type: 'list',
                name: 'variant',
                message: 'Select a variant',
                choices: [
                    "TypeScript",
                    {
                        name: 'JavaScript',
                        disabled: 'Unavailable at this time',
                    },
                ]
            }
        ])

        const repo = `${answers.framework}-${answers.variant}`
        const targetPath = path.join(process.cwd(), answers.projectName)

        if (repos[repo]) {
            const spinner = ora('Cloning repository...').start();
            clone(repos[repo], targetPath, {
                // checkout: 'main'
                checkout: 'master'
            }, (err) => {
                if (!err) {
                    spinner.succeed('Repository cloned!');
                    console.log('\n Done. Now run: \n')
                    console.log(`   cd ${answers.projectName}`)
                    console.log('   npm install')
                    console.log('   npm run dev')
                } else {
                    spinner.fail(`Clone failed: ${err.message}`);
                }
            });
        }

    } catch (err) {
        console.log('\n ✖ Operation cancelled')
        process.exit(0)
    }
}

// async function init () {
//
//     program
//         .command('create')
//         .description('使用该命令创建项目')
//         .action(start)
//
//     program.parse();
// }

start().catch((e) => {
    console.error(e)
    process.exit(1)
})